//
//  XZGForgetPasswordViewController.m
//  FastDoctor
//
//  Created by xiangzhigang on 16/5/21.
//  Copyright © 2016年 xiangzhi gang. All rights reserved.
//

#import "XZGForgetPasswordViewController.h"
#import "NSString+Verify.h"
#import <SMS_SDK/SMSSDK.h>
#import <SVProgressHUD/SVProgressHUD.h>
#import "XZGPasswordViewController.h"

@interface XZGForgetPasswordViewController (){
    
    UITextField *phone;
    UITextField *password;
}
@property (nonatomic, copy) NSString *phoneNumber;

@end

@implementation XZGForgetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"密码找回";
    self.view.backgroundColor = [UIColor colorWithRed:240/255.0f green:240/255.0f blue:240/255.0f alpha:1];
;
    self.navigationController.navigationBarHidden = NO;
    
    
    UIBarButtonItem *addBtn = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:self action:@selector(clickaddBtn)];
    [addBtn setImage:[UIImage imageNamed:@"goback_back_orange_on"]];
    [addBtn setImageInsets:UIEdgeInsetsMake(0, -15, 0, 15)];
    addBtn.tintColor=[UIColor colorWithRed:248/255.0f green:144/255.0f blue:34/255.0f alpha:1];
    [self.navigationItem setLeftBarButtonItem:addBtn];

    
    [self createButtons];
    [self createTextFields];
    
    
}
//创建按钮
- (void) createButtons {
    UIButton *next = [[UIButton alloc]initWithFrame:CGRectMake(50, 220, self.view.frame.size.width - 100, 30)];
    [next setTitle:@"下一步" forState:UIControlStateNormal];
    next.backgroundColor = NQNavColor;
    next.layer.cornerRadius = 5.0f;
    [next setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [next addTarget:self action:@selector(clickNext) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:next];
    
    UIButton *earnCode = [[UIButton alloc] initWithFrame:CGRectMake(self.view.frame.size.width - 130, 155, 80, 30)];
    [earnCode setTitle:@"获取验证码" forState:UIControlStateNormal];
    earnCode.backgroundColor = NQNavColor;
    earnCode.layer.cornerRadius = 5.0f;
    earnCode.titleLabel.font = [UIFont systemFontOfSize:12];

    
    [earnCode setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [earnCode setTitleColor:[UIColor grayColor] forState:UIControlStateHighlighted];
    [earnCode addTarget:self action:@selector(clickEarnCode) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:earnCode];
}

- (void)clickNext {
    
    if (password.text.length != 4) {
        [SVProgressHUD showErrorWithStatus:@"验证码输入有误"];
        return;
    }
    if (self.phoneNumber == nil) {
        [SVProgressHUD showErrorWithStatus:@"验证码没有获取成功，请重新获取"];
        return;
    }
    //提交验证码
    [SMSSDK commitVerificationCode:password.text phoneNumber:self.phoneNumber zone:@"86" result:^(NSError *error) {
        if (error) {
            [SVProgressHUD showErrorWithStatus:@"验证码错误"];
        } else{
            [SVProgressHUD showSuccessWithStatus:@"验证成功"];
             [self.navigationController pushViewController:[[XZGPasswordViewController alloc] init] animated:YES];
        }
    }];
    
}

- (void)clickEarnCode {
    if (!phone.text.isPhoneNumber) {
        [SVProgressHUD showErrorWithStatus:@"您输入的手机号有误"];
        return;
    }
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:phone.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
        if (!error) {
            self.phoneNumber = phone.text;
            [SVProgressHUD showSuccessWithStatus:@"获取验证码成功,请稍后查收"];
            
        } else {
            [SVProgressHUD showErrorWithStatus:@"获取验证码失败,请重试"];
        }
    }];
    
}
//创建注册文本框
- (void)createTextFields {
    phone = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, self.view.frame.size.width - 100, 40)];
    phone.placeholder = @"请输入您的手机号码";
    phone.keyboardType=UIKeyboardTypeNumberPad;
    phone.clearButtonMode = UITextFieldViewModeWhileEditing;
  
    phone.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_landing_nickname"]];
    phone.leftViewMode = UITextFieldViewModeAlways;
    phone.borderStyle = UITextBorderStyleRoundedRect;
    
    
    password = [[UITextField alloc] initWithFrame:CGRectMake(50, 150, self.view.frame.size.width - 200, 40)];
    password.keyboardType=UIKeyboardTypeNumberPad;
    password.clearButtonMode = UITextFieldViewModeWhileEditing;

    password.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mm_normal"]];
    password.leftViewMode = UITextFieldViewModeAlways;
    password.borderStyle = UITextBorderStyleRoundedRect;
    
    
    
    [self.view addSubview:phone];
    [self.view addSubview:password];
}

//返回
- (void)clickaddBtn {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
